package com.wn.web;

import com.wn.auth.JWTUtil;
import com.wn.common.Const;
import com.wn.common.PageParam;
import com.wn.ov.ConsultationOv;
import com.wn.ov.FormOv;
import com.wn.ov.UserOv;
import com.wn.pojo.Appointment;
import com.wn.pojo.Department;
import com.wn.pojo.User;
import com.wn.resopnse.ResponseCode;
import com.wn.resopnse.ServerResponse;
import com.wn.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 用户业务类
 * 注册
 * 登录
 * 预约
 */
@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 登录
     *
     *
     * @param user
     * @return
     */
    @PostMapping("loginWithUsernameAndPass")
    public ServerResponse login(@RequestBody UserOv user) {
        ServerResponse login = userService.login(user);
        return login;
    }

    /**
     * 注册
     *
     * @param user
     * @return
     */
    @PostMapping("register")
    public ServerResponse register(User user) {
        ServerResponse register = userService.register(user);
        return register;
    }

    /**
     * 预约前的数据准备（科室信息）
     */
    @PostMapping("departmentMsg")
    public ServerResponse departmentMsg() {
        List<Department> departments = userService.departmentMsg();

        return new ServerResponse(ResponseCode.SUCCESS,departments,"11");

    }

    /**
     * 预约前数据准备（医生信息）
     *
     * @param
     */
    @PostMapping("doctorMsg")
    public ServerResponse doctorMsg( @RequestBody User user) {
        List<User> userList = userService.doctorMsg(user.getPostId());
        return new ServerResponse(ResponseCode.SUCCESS,userList);
    }

    /**
     * 预约前数据准备（通过会诊申请获取）
     *
     * @param
     */
    @PostMapping("doctorMsgByConsultationOv")
    public ServerResponse doctorMsgByConsultationOv( @RequestBody ConsultationOv consultationOv) {

        List<User> userList = userService.doctorMsg(consultationOv.getDeptId());

        return new ServerResponse(ResponseCode.SUCCESS,userList);
    }
    /**
     * 预约前数据准备（通过当前登录用户获取）
     *
     * @param
     */
    @PostMapping("doctorMsgByCurrent")
    public ServerResponse doctorMsgByCurrent( @RequestHeader("token") String token) {
        UserOv user = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        List<User> userList = userService.doctorMsgByCurrent(user);
        return new ServerResponse(ResponseCode.SUCCESS,userList);
    }

    /**
     * 预约
     *
     * @param
     * @return
     */
    @PostMapping("addAppointment")
    public ServerResponse addAppointment(@RequestBody FormOv formOv,@RequestHeader("token") String token) {
        UserOv userOv = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        int id=userOv.getId();
        Appointment appointment=new Appointment();
        appointment.setDepartmentId(formOv.getDepartmentId());
        appointment.setDoctorUserId(formOv.getDoctorUserId());
        String time="";
        time=formOv.getDate()+"-"+formOv.getTime();
        appointment.setTime(time);
        appointment.setPatientId(id);
        return userService.addAppoint(appointment);

    }
    /**
     * 查看预约
     *
     * @param
     * @return
     */
    @PostMapping("getAppointment")
    public ServerResponse getAppointment(@RequestHeader("token") String token,@RequestBody PageParam pageParam) {
        UserOv userOv = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        return userService.getAppointment(userOv.getId(),pageParam);
    }
    /**
     * 预约审核撤销
     *
     * @param
     * @return
     */
    @PostMapping("updateAppointmentCancel")
    public ServerResponse updateAppointmentCancel(@RequestHeader("token") String token,@RequestBody Appointment appointment) {
        //UserOv userOv = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        //System.out.println("!!!!!!!!"+appointment);
        return userService.updateAppointmentCancel(appointment);
    }
    /**
     * 预约审核
     *
     * @param
     * @return
     */
    @PostMapping("updateAppointment")
    public ServerResponse updateAppointment(@RequestHeader("token") String token,@RequestBody Appointment appointment) {
        //UserOv userOv = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        return userService.updateAppointment(appointment);

    }
    /**
     * 删除预约
     *
     * @param
     * @return
     */
    @PostMapping("deleteAppointment")
    public ServerResponse deleteAppointment(@RequestHeader("token") String token,@RequestBody Appointment appointment) {
        return userService.deleteAppointment(appointment);
    }


    //获取token
    @GetMapping("getUserInfo")
    public ServerResponse getUserInfo(@RequestHeader("token") String token){
        UserOv userOv = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        ServerResponse serverResponse = userService.getUserInfo(userOv.getId());
        return serverResponse;
    }

    @PostMapping("logout")
    public ServerResponse logout(String token){
        return new ServerResponse(ResponseCode.SUCCESS,"登出成功");
    }

    /**
     * 用户在登录后进首页展示自己的个人信息
     * @param token
     */
    @GetMapping("getPatientMsg")
    public ServerResponse getPatientMsg( @RequestHeader("token") String token){
        UserOv userOv = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        int id=userOv.getId();
        User user = userService.getUserById(id);
        return  new ServerResponse(user,"获取成功");
    }

    /**
     * 用户信息
     * @param token
     * @return
     */
    @GetMapping("getUserMsgById")
    public ServerResponse getUserMsgById(@RequestHeader("token") String token){
        UserOv userOv = JWTUtil.parserToken(token, Const.PUBLIC_KEY);
        int id=userOv.getId();
        System.out.println(id+"===========================");
        ServerResponse userMsgById = userService.getUserMsgById(id);
        return userMsgById;
    }

}
